package ui.activity.mine.presenter;


import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import Bean.PresentRecordBean;
import Bean.PresentRecordParamBean;
import base.BaseBiz;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import ui.activity.mine.biz.PresentRecordBiz;
import ui.activity.mine.contract.PresentRecordContract;
import util.LogUtils;

/**
 * Created by chenjian on 19/2/26
 */

public class PresentRcordPresenter implements PresentRecordContract.Presenter {

    PresentRecordContract.View view;
    PresentRecordBiz presentRecordBiz;
    List<PresentRecordBean.ItemsBean> list = new ArrayList<>();


    @Inject
    public PresentRcordPresenter(PresentRecordContract.View view) {
        this.view = view;
    }


    @Override
    public void setBiz(BaseBiz biz) {
        presentRecordBiz = (PresentRecordBiz) biz;
    }


    @Override
    public void getList(boolean isRefresh, String id) {
        if (isRefresh) {
            list.clear();
        }
        view.loading(true);
        PresentRecordParamBean p = new PresentRecordParamBean();
        p.setCustomId(id);//Integer.parseInt(sp.getString("id", null))
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(p));
        presentRecordBiz.getList(requestBody, new BaseBiz.Callback<PresentRecordBean>() {
            @Override
            public void onSuccess(PresentRecordBean info) {
                list.addAll(info.getItems());
                view.upDateUI(list);
                view.hasLoadMore(false);
                if (isRefresh) {
                    view.refreshComplete();
                } else {
                    view.loadMoreComplete();
                }
                view.loading(false);
            }

            @Override
            public void onFailure(PresentRecordBean info) {
                view.loading(false);
                if (isRefresh) {
                    view.refreshComplete();
                } else {
                    view.loadMoreComplete();
                }
            }
        });

    }

    @Override
    public void getSelectList(String str) {
        if (list.size() == 0) {
            view.showMsg("您暂时没有提现记录");
            return;
        }
        List<PresentRecordBean.ItemsBean> beans = new ArrayList<>();
        for (PresentRecordBean.ItemsBean bean:list) {
            if (bean.getMon().equals(str)) {
                beans.add(bean);
            }
        }
//        for (int i = 0; i < list.size(); i++) {
//            if (list.get(i).getMon().equals(str)) {
//                beans.add(list.get(i));
//            }
//        }
        LogUtils.d(" list.size()"+ list.size());
        view.upDateSelect(beans);
    }
}
